package com.lz.service.fsh.impl;

import com.lz.entity.Car;
import com.lz.entity.Customer;
import com.lz.entity.Employee;
import com.lz.entity.InsuranceSheet;
import com.lz.repository.fsh.InsuranceSheetDao;
import com.lz.repository.fsh.impl.InsuranceImpl;
import com.lz.repository.fsh.vo.InsuranceSheetVo;
import com.lz.service.fsh.InsuranceSheetService;
import org.hibernate.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service

public class InsuranceSheetServiceImpl implements InsuranceSheetService {
    @Autowired
    private InsuranceSheetDao insuranceSheetDao;

    @Autowired
    private InsuranceImpl insuranceImpl;

    @Autowired
    SessionFactory sessionFactory;

    //查询所有车险单
    @Override
    public List<InsuranceSheetVo> getAlllist() {
        Session session = sessionFactory.openSession();
        List<InsuranceSheet> list = insuranceSheetDao.getAlllist(session);
        List<InsuranceSheetVo> list2 = new ArrayList<>();
        for (InsuranceSheet insuranceSheet2 : list) {
            InsuranceSheetVo equipmentSheetVO = new InsuranceSheetVo();
            equipmentSheetVO.setInsuranceSheetNo(insuranceSheet2.getInsuranceSheetNo());
            equipmentSheetVO.setCustomerName(insuranceSheet2.getCustomer().getCustomerName());
            equipmentSheetVO.setEmployeeId(insuranceSheet2.getEmployee().getEmployeeId());
            equipmentSheetVO.setEffectiveTime(insuranceSheet2.getEffectiveTime());
            equipmentSheetVO.setInvalidTime(insuranceSheet2.getInvalidTime());
            equipmentSheetVO.setHandlingFee(insuranceSheet2.getHandlingFee());
            equipmentSheetVO.setRegistrationStatus(insuranceSheet2.getRegistrationStatus());
            equipmentSheetVO.setInsuranceCompany(insuranceSheet2.getInsuranceCompany());
            list2.add(equipmentSheetVO);
        }
        session.close();
        // TODO Auto-generated method stub
        return list2;
    }

    @Override
    public List<Car> getAlllist1() {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        List<Car> list = insuranceSheetDao.getList(session);

        transaction.commit();
        session.close();
        // TODO Auto-generated method stub
        return list;
    }

    //添加车险单
    @Override
    public Integer addInsuranceSheet(InsuranceSheet insuranceSheet, Customer customer, Car car, Employee emloyee) {
        //开启事务
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        insuranceSheet.setCar(car);
        insuranceSheet.setCustomer(customer);
        insuranceSheet.setEmployee(emloyee);

        insuranceSheetDao.addCar(session, car);
        insuranceSheetDao.addCustomer(session, customer);
        insuranceSheetDao.addEmployee(session, emloyee);
        insuranceSheetDao.addInsuranceSheet(session, insuranceSheet);
        //提交事务
        transaction.commit();
        session.close();
        return null;
    }

    //条件查询
    @Override
    public List<InsuranceSheetVo> selectInsuranceSheet(InsuranceSheetVo insuranceSheetVo, Integer pageNo, Integer pageSize) {
        Session session = sessionFactory.openSession();
        StringBuffer sql = new StringBuffer("select * from InsuranceSheet ins where 1=1 ");

        //判断公司名是否为空
        if (insuranceSheetVo.getInsuranceCompany() != null && !"".equals(insuranceSheetVo.getInsuranceCompany())) {
            sql.append(" and ins.insuranceCompany like '%" + insuranceSheetVo.getInsuranceCompany() + "%'");
        }
        //判断客户名是否为空
       /* if(insuranceSheetVo.getCustomerName()!=null && !"".equals(insuranceSheetVo.getCustomerName())){
            sql.append(" and ins.");
        }*/

        //判断登记状态
        if (insuranceSheetVo.getRegistrationStatus() != null && !"".equals(insuranceSheetVo.getRegistrationStatus())) {
            sql.append(" and ins.registrationStatus like '%" + insuranceSheetVo.getRegistrationStatus() + "%'");
        }
        SQLQuery query = session.createSQLQuery(sql.toString());
        query.setFirstResult((pageNo - 1) * pageSize);
        query.setMaxResults(pageSize);

        List<InsuranceSheet> list = insuranceSheetDao.selectInsuranceSheet(query);
        System.out.println(list.toString());
        List<InsuranceSheetVo> list2 = new ArrayList<>();
        for (InsuranceSheet insuranceSheet2 : list) {
            InsuranceSheetVo equipmentSheetVO = new InsuranceSheetVo();
            equipmentSheetVO.setInsuranceSheetNo(insuranceSheet2.getInsuranceSheetNo());
            equipmentSheetVO.setCustomerName(insuranceSheet2.getCustomer().getCustomerName());
            equipmentSheetVO.setEmployeeId(insuranceSheet2.getEmployee().getEmployeeId());
            equipmentSheetVO.setEffectiveTime(insuranceSheet2.getEffectiveTime());
            equipmentSheetVO.setInvalidTime(insuranceSheet2.getInvalidTime());
            equipmentSheetVO.setHandlingFee(insuranceSheet2.getHandlingFee());
            equipmentSheetVO.setRegistrationStatus(insuranceSheet2.getRegistrationStatus());
            equipmentSheetVO.setInsuranceCompany(insuranceSheet2.getInsuranceCompany());
            list2.add(equipmentSheetVO);
        }
        session.close();

        return list2;
    }

    //获取订单总条数
    @Override
    public Integer rowsInsuranceSheet(InsuranceSheetVo insuranceSheetVo) {
        Session session = sessionFactory.openSession();
        StringBuffer sql = new StringBuffer("select count(1) from InsuranceSheet ins where 1=1");

        //判断公司名称是否为空
       /* if (insuranceSheetVo.getInsuranceCompany() != null && !"".equals(insuranceSheetVo.getInsuranceCompany())) {
            //客户姓名不为空
            if (insuranceSheetVo.getCustomerName()!=null && !"".equals(insuranceSheetVo.getCustomerName()))
            {//客户名和公司名同时不为空的时候
                hql.append("and ins.insuranceCompany like :insuranceCompany and ins.customer.customerName like :customerName " +
                        "and ins.registrationStatus like :registrationStatus");
                query=session.createQuery(hql.toString());
                query.setString("customerName", "%"+insuranceSheetVo.getCustomerName()+"%");
                query.setString("insuranceCompany", "%"+insuranceSheetVo.getInsuranceCompany()+"%");
                query.setString("registrationStatus","%"+insuranceSheetVo.getRegistrationStatus()+"%");
            }else {
                //客户名字为空公司名不为空时
                if(insuranceSheetVo.getInsuranceCompany()!=null && !"".equals(insuranceSheetVo.getInsuranceCompany())){
                hql.append(" and ins.insuranceCompany like :insuranceCompany and ins.registrationStatus like :registrationStatus");
                query.setString("insuranceCompany", "%"+insuranceSheetVo.getInsuranceCompany()+"%");
                query.setString("registrationStatus","%"+insuranceSheetVo.getRegistrationStatus()+"%");
                }
            }
        } else {
            //公司名为空时客户名不为空的时候
            if(insuranceSheetVo.getCustomerName()!=null && !"".equals(insuranceSheetVo.getCustomerName())){
            hql.append(" and ins.customer.customerName like :customerName " +
                    " and ins.registrationStatus like :registrationStatus");
            query=session.createQuery(hql.toString());
            query.setString("customerName", "%"+insuranceSheetVo.getCustomerName()+"%");

            query.setString("registrationStatus","%"+insuranceSheetVo.getRegistrationStatus()+"%");
            }
        }*/
        //判断公司名是否为空
        if (insuranceSheetVo.getInsuranceCompany() != null && !"".equals(insuranceSheetVo.getInsuranceCompany())) {
            sql.append(" and ins.insuranceCompany like '%" + insuranceSheetVo.getInsuranceCompany() + "%'");
        }
        //判断客户名是否为空
       /* if(insuranceSheetVo.getCustomerName()!=null && !"".equals(insuranceSheetVo.getCustomerName())){
            sql.append(" and ins.");
        }*/

        //判断登记状态
        if (insuranceSheetVo.getRegistrationStatus() != null && !"".equals(insuranceSheetVo.getRegistrationStatus())) {
            sql.append(" and ins.registrationStatus like '%" + insuranceSheetVo.getRegistrationStatus() + "%'");
        }
        SQLQuery query = session.createSQLQuery(sql.toString());
        Integer rows = insuranceSheetDao.rowsInsuranceSheet(query);
        System.out.print(rows);
        session.close();
        return rows;

    }

    //条件查询map判断获得总条数
    @Override
    public Integer rowsInsuranceSheet2(Map map) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        StringBuffer stringBuffer = new StringBuffer(" from InsuranceSheet ins where 1=1");
        //通过迭代器遍历map
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry next = (Map.Entry<String, String>) iterator.next();
            String key = (String) next.getKey();
            String value = (String) next.getValue();
            if (!"".equals(value) && value != null) {
                stringBuffer.append(" and ins.");
                stringBuffer.append(key);
                stringBuffer.append(" like '%");
                stringBuffer.append(value);
                stringBuffer.append("%'");
            }
        }
        Query query = session.createQuery(stringBuffer.toString());
        Integer rows = insuranceSheetDao.rowsInsuranceSheet(query);
        transaction.commit();
        session.close();
        return rows;
    }

    //条件查询map判断
    @Override
    public List<InsuranceSheet> selectIns(Integer pageNo, Integer pageSize, Map map) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        StringBuffer stringBuffer = new StringBuffer(" from InsuranceSheet ins where 1=1");

        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry next = (Map.Entry<String, String>) iterator.next();
            String key = (String) next.getKey();
            String value = (String) next.getValue();
            if (!"".equals(value) && value != null) {
                stringBuffer.append(" and ins.");
                stringBuffer.append(key);
                stringBuffer.append(" like '%");
                stringBuffer.append(value);
                stringBuffer.append("%'");
            }
        }
        Query query = session.createQuery(stringBuffer.toString());
        query.setFirstResult((pageNo - 1) * pageSize);
        query.setMaxResults(pageSize);
        List<InsuranceSheet> list = insuranceSheetDao.selectInsuranceSheet(query);
        transaction.commit();
        session.close();

        return list;
    }

    //条件查询map集合判断

    //删除数据
    @Override
    public Integer delInsuranceSheet(InsuranceSheet insuranceSheet) {
        //开启session
        Session session = sessionFactory.openSession();
        //开启事务
        Transaction transaction = session.beginTransaction();
        InsuranceSheet insuranceSheet1 = new InsuranceSheet();
        insuranceSheet1.setInsuranceSheetNo(insuranceSheet.getInsuranceSheetNo());
        session.delete(insuranceSheet1);
        transaction.commit();
        session.close();
        return null;
    }

    //根据id主键查询
    @Override
    public Map<String, Object> findByInsuranceSheet(Integer insuranceSheetNo) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        String hql = "from InsuranceSheet ins where ins.insuranceSheetNo = " + String.valueOf(insuranceSheetNo);
        //查询对应得订单
        Query query = session.createQuery(hql);
        InsuranceSheet insuranceSheet = (InsuranceSheet) query.uniqueResult();
        Map<String, Object> map = new HashMap<>();
        map.put("insuranceSheet", insuranceSheet);
        map.put("employee", insuranceSheet.getEmployee());
        map.put("customer", insuranceSheet.getCustomer());
        transaction.commit();
        session.close();
        return map;
    }

    //传入值进行修改
    @Override
    public void changeinsurancesheet(InsuranceSheetVo insuranceSheetVo) {
        //开启session
        Session session = sessionFactory.getCurrentSession();
        Integer insuranceSheetNo = insuranceSheetVo.getInsuranceSheetNo();
        Map<String, Object> map = findByInsuranceSheet(insuranceSheetNo);
        InsuranceSheet insuranceSheet = (InsuranceSheet) map.get("InsuranceSheet");
        session.save(insuranceSheet);
        Customer customer = (Customer) map.get("customer");
        session.save(customer);
        Employee employee = (Employee) map.get("employee");
        session.save(employee);
        insuranceSheet.setRegistrationStatus(insuranceSheetVo.getRegistrationStatus());
        insuranceSheet.setInsuranceCompany(insuranceSheetVo.getInsuranceCompany());
        insuranceSheet.setHandlingFee(insuranceSheetVo.getHandlingFee());
        customer.setCustomerName(insuranceSheetVo.getCustomerName());
        employee.setEmployeeId(insuranceSheetVo.getEmployeeId());
        // insuranceSheet.setEffectiveTime(insuranceSheetVo.getEffectiveTime());
        // insuranceSheet.setInvalidTime(insuranceSheetVo.getInvalidTime());
        session.update(customer);
        session.update(employee);
        session.update(insuranceSheet);
        session.flush();
        session.close();
    }

    //test修改
    @Override
    public void changeins(InsuranceSheetVo insuranceSheetVo) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        InsuranceSheet insuranceSheet = (InsuranceSheet) session.get(InsuranceSheet.class, insuranceSheetVo.getInsuranceSheetNo());
        Customer customer1 = insuranceSheet.getCustomer();
        session.save(customer1);
        customer1.setCustomerName(insuranceSheetVo.getCustomerName());

        insuranceSheet.setInvalidTime(insuranceSheetVo.getInvalidTime());
        insuranceSheet.setEffectiveTime(insuranceSheetVo.getEffectiveTime());
        insuranceSheet.setHandlingFee(insuranceSheetVo.getHandlingFee());
        insuranceSheet.setInsuranceCompany(insuranceSheetVo.getInsuranceCompany());
        insuranceSheet.setRegistrationStatus(insuranceSheetVo.getRegistrationStatus());
        //customer1.setCustomerName(insuranceSheetVo.getCustomerName());

        session.update(insuranceSheet);
        session.update(customer1);
        transaction.commit();
        session.close();
    }

    @Override
    public void changeinsstste(InsuranceSheetVo insuranceSheetVo) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        InsuranceSheet insuranceSheet = (InsuranceSheet) session.get(InsuranceSheet.class, insuranceSheetVo.getInsuranceSheetNo());
        insuranceSheet.setInsuranceSheetState(insuranceSheetVo.getInsuranceSheetState());
        session.update(insuranceSheet);
        transaction.commit();
        session.close();
    }
     //最大id
    @Override
    public Integer getMaxPrimaryKey() {
        return insuranceSheetDao.getMaxPrimaryKey();
    }

}
